/// <reference path="../types.d.ts" />
import { isVue2, isVue3 } from "vue-demi";
import Exposure from "./Exposure";

export default {
	install(context: any, options: ExposureOptions = {}) {
		if (typeof window === "undefined") return;
		const service = new Exposure(options)
		let lifeCycle: VueDirectiveLifecycle = {}
		if (isVue2) {
			context.prototype.$Exposure = service
			lifeCycle = {
				inserted(el: Element, { value, modifiers }: any) {
					service.addEl(el, value, modifiers)
				},
				componentUpdated(el: Element, { value, oldValue, modifiers }: any) {
					service.updateEl(el, value, oldValue, modifiers)
				},
				unbind(el: Element) {
					service.removeEl(el)
				}
			}
		} else if (isVue3) {
			context.config.globalProperties.$Exposure = service
			lifeCycle = {
				mounted(el: Element, { value, modifiers }: any) {
					service.addEl(el, value, modifiers)
				},
				updated(el: Element, { value, oldValue, modifiers }: any) {
					service.updateEl(el, value, oldValue, modifiers)
				},
				unmounted(el: Element) {
					service.removeEl(el)
				}
			}
		} else {
			console.warn(`插件不支持当前Vue框架版本：${context.version}，请使用Vue2/Vue3`);
			return
		}
		context.directive("exposure", lifeCycle);
	},
};
